﻿using System.Drawing.Imaging;
using System.Drawing;

namespace DFGK_WMS.WebAPI.Tools
{
    public static class Utils
    {
        /// <summary>
        /// 生成四位随机数字和字母验证码
        /// </summary>
        /// <returns></returns>
        public static string GenerateRandomCaptcha()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var captcha = new char[4];
            var random = new Random();
            for (int i = 0; i < captcha.Length; i++)
            {
                captcha[i] = chars[random.Next(chars.Length)];
            }
            return new string(captcha);
        }
        /// <summary>
        /// 生成图片
        /// </summary>
        /// <param name="captchaValue">验证码的值</param>
        /// <returns></returns>
        public static byte[] GetCaptchaImage(string captchaValue)
        {
            using var img = new Bitmap(80, 30);
            using var graphics = Graphics.FromImage(img);
            using var font = new Font("Arial", 16);
            using var brush = new SolidBrush(Color.Black);
            graphics.DrawString(captchaValue, font, brush, 10, 5);
            using var stream = new MemoryStream();
            img.Save(stream, ImageFormat.Png);
            return stream.ToArray();
        }
    }
}
